package framework;

import java.util.LinkedList;

import org.newdawn.slick.GameContainer;

import framework.attack.BasicAttack;

public class AttackBuffer implements Buffer<BasicAttack<BasicFighter>> {
	
	protected LinkedList<BufferedAttack> q;
	
	private int conservationTime;
	
	public AttackBuffer() {
		q = new LinkedList<>();
		conservationTime = 100;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		for (int i=0; i < q.size(); i++) {
			q.get(i).update(gc, delta);
			if (q.get(i).getCurrentTime() > getConservationTime()) {
				q.remove(i);
			}
		}
	}

	@Override
	public int getConservationTime() {
		return conservationTime;
	}

	@Override
	public void setConservationTime(int conservationTime) {
		this.conservationTime = conservationTime;
	}

	@Override
	public void buffer(BasicAttack<BasicFighter> element) {
		q.push(new BufferedAttack(element));
	}

	@Override
	public BasicAttack<BasicFighter> consume() {
		return q.size() > 0 ? q.pop().getAttack() : null;
	}
	
	public class BufferedAttack implements Updatable {
		
		private int currentTime;
		
		private BasicAttack<BasicFighter> attack;

		public BufferedAttack(BasicAttack<BasicFighter> attack) {
			currentTime = 0;
			this.attack = attack;
		}
		
		public int getCurrentTime() {
			return currentTime;
		}

		public BasicAttack<BasicFighter> getAttack() {
			return attack;
		}

		public void setCurrentTime(int currentTime) {
			this.currentTime = currentTime;
		}

		public void setAttack(BasicAttack<BasicFighter> attack) {
			this.attack = attack;
		}

		@Override
		public void update(GameContainer gc, int delta) {
			this.currentTime += delta;
		}	
	}

	@Override
	public BasicAttack<BasicFighter> getTop() {
		return null;
	}

	@Override
	public boolean contains(BasicAttack<BasicFighter> element) {
		return q.contains(element);
	}

	@Override
	public void remove(BasicAttack<BasicFighter> element) {
		// TODO Auto-generated method stub
		
	}
}
